ngx.req.read_body()
local raw = ngx.req.get_body_data()
local json = require "cjson"
local args

pcall(function(raw) args = json.decode(raw) end, raw)

if nil == args then
    ngx.err("no post data")
    return
end

if nil == args.act_id then
    ngx.say('{"code":10000,"message":"act_id不能为空"}')
    return
end

if nil == args.ids then
    ngx.say('{"code":10000,"message":"ids不能为空"}')
    return
end

local act_id = args.act_id;
local redis_client = require "lib.redis"
local redis = redis_client:new()

local ids = args.ids
local store_keys = {}
local ranking_key = "ranking:"..act_id
local scores = {}

for _, id in ipairs(ids) do
    table.insert(store_keys, 'item:'..act_id..':'..id)
    local score = redis:zscore(ranking_key, id) or 0
    table.insert(scores, string.format('"%s":%s', id, score))
end

local items, err = redis:mget(unpack(store_keys))

if nil == items then
    ngx.say('{"code":10000,"message":"没有搜索到结果"}')
    return
end

ngx.say('{"code":200,"message":"success","data":{')
ngx.say('"items":[', table.concat(items, ',') , '],')
ngx.say('"scores":{', table.concat(scores, ','), '}')
ngx.say('}}')
